package com.combetter.changxing.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.FrameLayout;

import com.combetter.changxing.MainActivity;
import com.combetter.changxing.MyApplication;
import com.combetter.changxing.R;

import java.util.HashMap;


/**
 * @author VanyLiu
 */

public class LaunchActivity extends Activity {
    private FrameLayout frameLayout;
    public static final String IS_FIRST = "is_first";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_launch);
        frameLayout = findViewById(R.id.flContent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();
    }

    private void initView() {
        AlphaAnimation animation = new AlphaAnimation(0.5f, 1f);
        animation.setDuration(2000);
        frameLayout.startAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (true) {
                    startActivity(new Intent(LaunchActivity.this, MainActivity.class));
                    finish();
//                    if (MyApplication.getInstance().getUserInfo() != null) {
//                        MyApplication.getInstance().resetUserInfo();
//                        startActivity(new Intent(LaunchActivity.this, MainActivity.class));
//                        finish();
//                    } else {
//                        startActivity(new Intent(LaunchActivity.this, LoginActivity.class));
//                        finish();
//                    }
                    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                } else {
                    // startActivity(new Intent(LaunchActivity.this, GuideActivity.class));
                }
            }
        });
    }

    /**
     * 自动登录
     */
    public void login() {
        HashMap<String, String> map = new HashMap<>();
//        map.put("token", MyApp.getInstance().getUserInfo().getToken());
//        HttpRequest.getInstance().createService(APIService.class)
//                .login(map)
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribeOn(Schedulers.io())
//                .doOnError(throwable -> {
//                    LogUtil.e(throwable.getMessage());
//                    startActivity(new Intent(LaunchActivity.this, LoginActivity.class));
//                    finish();
//                })
//                .subscribe(userHttpResult -> {
//                    LogUtil.e("自动登录返回：", userHttpResult);
//                    if (userHttpResult.getCode() == 200) {
//                        User user = MyApp.getInstance().getUserInfo();
//                        User newUser = userHttpResult.getData();
//                        user.setManager_mail(newUser.getManager_mail());
//                        user.setManager_phone(newUser.getManager_phone());
//                        user.setToken(newUser.getToken());
//                        MyApp.getInstance().getDatabase().userDao().insertUser(user);
//                        startActivity(new Intent(LaunchActivity.this, MainActivity.class));
//                        finish();
//                    } else {
//                        ToastUtils.error(userHttpResult.getMsg());
//                        startActivity(new Intent(LaunchActivity.this, LoginActivity.class));
//                        finish();
//                    }
//                });
    }
}
